package com.zmn.oms.dubbo.impl.zmn.serviceprovider.part;

import org.apache.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.ExpressRecordDRO;
import com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.ProgressSpeedDRO;
import com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.SpPartDetailDRO;
import com.zmn.oms.dubbo.interfaces.zmn.serviceprovider.part.ServProviderPartRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.fcorderpart.detail.FcOrderPartDetail;
import com.zmn.oms.model.entity.fcorderpart.detail.FcOrderPartPostDetail;
import com.zmn.oms.zmn.business.interfaces.part.ZsOrderFactoryPartBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：配件管理
 *
 * @author wangxiaokun
 * @since 2020/9/10 14:31
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ServProviderPartRemoteServiceImpl implements ServProviderPartRemoteService {

    @Autowired
    ZsOrderFactoryPartBService orderFactoryPartBService;

    /**
     * 通过订单配件id、物流信息id和配件业务查询配件详情和物流信息
     * @param fcOrderPartId
     * @param applyType
     * @return com.zmn.common.dto2.ResponseDTO<com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.SpPartDetailDRO>
     * @author wangxiaokun
     */
    @Override
    public ResponseDTO<SpPartDetailDRO> findPartDetail(Integer fcOrderPartId, Integer applyType) {
        log.info("服务商配件详情查询，配件单号:[{}]，物流信息id:[{}]", fcOrderPartId);
        try {
            FcOrderPartDetail detail = orderFactoryPartBService.detail(fcOrderPartId);

            SpPartDetailDRO spPartDetailDRO = BeanMapper.map(detail, SpPartDetailDRO.class);
            spPartDetailDRO.setPartImg(detail.getImgSrc1());

            // 配件进度状态集合
            List<ProgressSpeedDRO> stateList = new ArrayList<>(4);
            if (!Objects.equals(applyType, 4)){
                stateList.add(ProgressSpeedDRO.builder().title("申请提交").build());
                stateList.add(ProgressSpeedDRO.builder().title("厂商审批").build());
                stateList.add(ProgressSpeedDRO.builder().title("厂商发件").build());
                stateList.add(ProgressSpeedDRO.builder().title("用户已收货").build());
            } else {
                stateList.add(ProgressSpeedDRO.builder().title("旧件已邮寄").build());
                stateList.add(ProgressSpeedDRO.builder().title("厂商确认旧件").build());
            }
            int active = 0;
            do {

                //如果配件是新件，123新件，4旧件
                if (!Objects.equals(applyType, 4)){
                    active = 1;
                    // 申请提交时间
                    stateList.get(0).setDate(spPartDetailDRO.getCreateTime());

                    //如果审核时间不为空
                    if (Objects.nonNull(detail.getReviewTime())){
                        active = 2;
                        // 厂商审批时间
                        stateList.get(1).setDate(detail.getReviewTime());
                    }
                }

                if (CollectionUtil.isNullOrEmpty(detail.getPostInfo())){
                    break;
                }

                List<FcOrderPartPostDetail> postDetails = null;
                // 根据获取新件或旧件物流
                if (Objects.equals(applyType, 4)){
                    postDetails = detail.getPostInfo().stream()
                            .filter(post -> Objects.equals(post.getFcOrderPartPostType(), GlobalConsts.YES))
                            .collect(Collectors.toList());
                } else {
                    postDetails = detail.getPostInfo().stream()
                            .filter(post -> Objects.equals(post.getFcOrderPartPostType(), GlobalConsts.NO))
                            .collect(Collectors.toList());
                }
                if (CollectionUtil.isNullOrEmpty(postDetails)){
                    break;
                }

                //物流信息
                FcOrderPartPostDetail fcOrderPartPostDetail = postDetails.get(0);
                spPartDetailDRO.setPostAddressType(fcOrderPartPostDetail.getPostAddressType());
                spPartDetailDRO.setAddress(fcOrderPartPostDetail.getAddress());
                spPartDetailDRO.setConsigneeName(fcOrderPartPostDetail.getConsigneeName());
                spPartDetailDRO.setConsigneePhone(fcOrderPartPostDetail.getConsigneePhone());
                spPartDetailDRO.setExpressCompany(fcOrderPartPostDetail.getExpressCompany());
                spPartDetailDRO.setExpressCode(fcOrderPartPostDetail.getExpressCode());

                if (Objects.isNull(spPartDetailDRO.getExpressCode())){
                    break;
                }

                //如果配件是新件，123新件，4旧件
                if (!Objects.equals(applyType, 4)){
                    active = 3;
                    // 厂商发件时间
                    stateList.get(2).setDate(fcOrderPartPostDetail.getExpressTime());
                } else {
                    active = 1;
                    // 旧件邮寄时间
                    stateList.get(0).setDate(fcOrderPartPostDetail.getExpressTime());
                }

                if (Objects.equals(fcOrderPartPostDetail.getExpressData(), "0")){
                    break;
                }
                // 快递记录
                List<ExpressRecordDRO> expressRecordDROS = JSONObject.parseArray(fcOrderPartPostDetail.getExpressData(),
                        ExpressRecordDRO.class);

                if (CollectionUtil.isNullOrEmpty(expressRecordDROS)){
                    break;
                }
                spPartDetailDRO.setExpressRecord(expressRecordDROS);

                if (Objects.equals(fcOrderPartPostDetail.getStatus(), 4)){
                    //如果配件是新件，123新件，4旧件
                    if (!Objects.equals(applyType, 4)){
                        active = 4;
                        // 用户签收时间
                        stateList.get(3).setDate(expressRecordDROS.get(0).getDate());
                    } else {
                        active = 2;
                        // 厂商确认旧件时间
                        stateList.get(1).setDate(expressRecordDROS.get(0).getDate());
                    }
                }
            }while (false);

            spPartDetailDRO.setStateList(stateList);
            spPartDetailDRO.setActive(active);

            return ResponseDTO.success(spPartDetailDRO);
        } catch (Exception e){
            log.error("[{}] 查询详情错误：{}", "配件", e);
            return ResponseDTO.fail(e.getMessage());
        }
    }
}
